<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Hash;
use DB;
class RegisterController extends Controller
{
    //用户注册
    public function register(Request $request)
    {
    	if($request -> isMethod('POST'))
		{
			$this -> validate($request,[
				'email' => 'required|email|unique:users',
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
				],[
				'email.required' => '邮箱不能为空',
				'email.email' => '邮箱不合法',
				'password.required' => '密码不能为空',
				'password.confirmed' => '确认密码不一致',
				'password_confirmation.required' => '确认密码不能为空',
				'email.unique' => '该邮箱已经被注册',
				]);
			$data = $request -> except('_token','password_confirmation','check');
			$data['password'] = Hash::make($data['password']);
			$time = time();
			$data['add_time'] = $time;
			$data['update_time'] = $time;
			$data['remember_token'] = str_random(50);
			$data['name'] = str_random(7);
			$res = DB::table('users') -> insertGetId($data);
			DB::table('userdetail') -> insert(['uid' => $res]);

			if($res)
			{
				return redirect('/admin/index') -> with(['info' => '注册成功']);
			}else
			{
				return back() -> with(['info' => '注册失败']);
			}
		}
    	return view('admin.register');
    }
}
